/*******************************************************************************
* FileName : stack.h
* Comment  : 栈
* Version  : 1.0
* Author   : jemmy
* Date     : 2013-07-07
*******************************************************************************/
#ifndef _STACK_H_INCLUDED
#define _STACK_H_INCLUDED

template<typename T>
struct TStackNode 
{
	TStackNode()
	{
		memset( this, 0, sizeof(TStackNode) );
	}

	T val;						
	TStackNode<T>* pNext;		
};

template<class T>
class CStack 
{
public:
	CStack();

	int		Size();				
	bool	IsEmpty();		
	T		GetTop();			// 得到栈顶	
	void	Push(const T& val);	// 入栈
	T		Pop();				// 出栈

private:
	TStackNode<T>* m_pHead;
	int m_nSize;				// count
};

template<class T>
CStack<T>::CStack() 
{
	m_nSize = 0;
	m_pHead = NULL;
}

template<class T>
inline int CStack<T>::Size() 
{
	return m_nSize;
}

template<class T>
inline bool CStack<T>::IsEmpty() 
{
	return m_nSize == 0;
}

template<class T>
T CStack<T>::GetTop() 
{
	if ( IsEmpty() ) 
	{
		cout<<"empty stack"<<endl;
		return NULL;
	}

	return m_pHead->val;
}

template<class T>
void CStack<T>::Push(const T& val) 
{
	TStackNode<T>* pOldHead = m_pHead;
	m_pHead = new TStackNode<T>;
	m_pHead->val = val;
	m_pHead->pNext = pOldHead;
	++m_nSize;
}

template<class T>
T CStack<T>::Pop() 
{
	if ( IsEmpty() ) 
	{
		cout<<"empty stack"<<endl;
		return NULL;
	}

	T tRet = m_pHead->val;
	TStackNode<T>* pNewHead = m_pHead->pNext;
	delete m_pHead;
	m_pHead = pNewHead;

	--m_nSize;

	return tRet;
}


int main_stack();

#endif